#include "ADC.h"

u32 ADCvalue = 0;

void ADC_E11_Init(void)
{
        ADC_InitTypeDef                      ADC_InitStructure;
        GPIO_InitTypeDef                     GPIO_InitStructure;
        ADC_CommonInitTypeDef                ADC_CommonInitStructure;
        NVIC_InitTypeDef                     NVIC_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
        
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        //GPIO_InitStructure.GPIO_OType = 
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_Init(GPIOE,&GPIO_InitStructure);
        
        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
        //ADC_InitStructure.ADC_ExternalTrigConv = 
        ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; 
        ADC_InitStructure.ADC_NbrOfConversion = 1;
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
        ADC_InitStructure.ADC_ScanConvMode = DISABLE;        
        ADC_Init(ADC1,&ADC_InitStructure);
        
        ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
        ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
        ADC_CommonInit(&ADC_CommonInitStructure);
        ADC_Cmd(ADC1,ENABLE);
        ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
        
        ADC_RegularChannelConfig(ADC1,ADC_Channel_5,1,ADC_SampleTime_480Cycles);
				ADC_SoftwareStartConv(ADC1);
        
        NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
        NVIC_Init(&NVIC_InitStructure);
}

u32 tempValue = 0;
void ADC_IRQHandler(void)
{
        
        if(ADC_GetITStatus(ADC1,ADC_IT_EOC) != RESET)
        {
                tempValue =  ADC_GetConversionValue(ADC1);
        }
        ADCvalue = tempValue;
        ADC_ClearITPendingBit(ADC1,ADC_IT_EOC);
}

